import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.URL;

import org.apache.commons.io.FileUtils;


public class MOSSCheck {

	public static void main(String[] args) {
		// a list of students' source code files located in the prepared
					// directory.
		SocketClient socketClient = new SocketClient();
		try {
					Collection<File> files = FileUtils.listFiles(new File(
						"c:\\ProgFiles\\apps"), new String[] { "java" }, true);

					// a list of base files that was given to the students for this
					// assignment.
					Collection<File> baseFiles = FileUtils.listFiles(new File(
						"C:\\ProgFiles\\base"), new String[] { "java" }, true);
					
					//get a new socket client to communicate with the Moss server
					//and set its parameters.
					
					
					//set your Moss user ID
					socketClient.setUserID("897506485");
					//socketClient.setOpt...
					
					//set the programming language of all student source codes
					try {
						socketClient.setLanguage("java");
					} catch (MossException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					//initialize connection and send parameters
					socketClient.run();
					
					// upload all base files
					for (File f : baseFiles) {
						System.out.println("Base File uploading::"+f.getName());
						socketClient.uploadBaseFile(f);
					}
					
					//upload all source files of students
					for (File f : files) {
						System.out.println("File uploading::"+f.getName());
						socketClient.uploadFile(f);
					}
					
					//finished uploading, tell server to check files
					System.out.println("Server " + socketClient.getServer());
					socketClient.sendQuery();
					
					//get URL with Moss results and do something with it
					URL results = socketClient.getResultURL();
					System.out.println("Results available at " + results.toString());
					
					System.out.println("Percent similarity:::"+fetchResults(results.toString()));
		}catch(Exception E){
			E.printStackTrace();
		}finally{
			socketClient.close();
		}

	}
	
	public static int fetchResults(String URL) throws Exception {

    	String result=URL;
    	//String result1="http://testapp-utd.rhcloud.com/PerlJava";
        URL oracle = new URL(result);
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));
        String inputLine;
        int percentDuplicate = 0;
        while ((inputLine = in.readLine()) != null)
        {
        	String fileName;    
        	String re1=".*?";	// Non-greedy match on filler
            String re2="(deidei\\.java)";	// Fully Qualified Domain Name 1
            String txt=inputLine;
            Pattern p = Pattern.compile(re1+re2,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
            Matcher m = p.matcher(txt);
            if (m.find())
            {
                String fqdn1=m.group(1);
                System.out.println(inputLine);
                fileName=fqdn1.toString();
                
                String reg1=".*?";	// Non-greedy match on filler
        	    String reg2="(\\([0-9]+%\\))";	// Round Braces 1
        	    String txt2=inputLine;
        	    Pattern pa = Pattern.compile(reg1+reg2,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
        	    Matcher ma = pa.matcher(txt2);
        	    if (ma.find())
        	    {
        	    	
        	    	String print="";
        	        String rbraces1=ma.group(1);
        	        String intermediate=rbraces1.toString();
        	        int length=intermediate.length();
        	        if(length==5){
        	        print=intermediate.substring(1, 3);}
        	        else if(length==6){
        	        	print=intermediate.substring(1,4);
        	        }
        	        else if(length==4){
        	        	
        	        	print=intermediate.substring(1,2);
        	        }
        	        percentDuplicate=Integer.parseInt(print);
        	        System.out.println(fileName+"-"+percentDuplicate);
        	    }
            }
        	
        	     ///*else{*/System.out.println("NM");//}
        }
        in.close();
        return percentDuplicate;
    }

}
